'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('clothing', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '用户ID',
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      category_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '分类ID',
        references: {
          model: 'categories',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '衣服名称',
      },
      brand: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '品牌',
      },
      model: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '型号',
      },
      size: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: '尺码',
      },
      color: {
        type: Sequelize.STRING(30),
        allowNull: true,
        comment: '颜色',
      },
      color_hex: {
        type: Sequelize.STRING(7),
        allowNull: true,
        comment: '颜色十六进制值',
      },
      material: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '材质',
      },
      price: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: true,
        comment: '价格',
      },
      purchase_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '购买日期',
      },
      purchase_place: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '购买地点',
      },
      season: {
        type: Sequelize.ENUM('spring', 'summer', 'autumn', 'winter', 'all'),
        allowNull: true,
        comment: '适用季节',
      },
      style: {
        type: Sequelize.ENUM('casual', 'formal', 'business', 'sport', 'party', 'vintage', 'street'),
        allowNull: true,
        comment: '风格',
      },
      occasion: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '适用场合',
      },
      images: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '图片URLs',
      },
      main_image: {
        type: Sequelize.STRING(255),
        allowNull: true,
        comment: '主图片URL',
      },
      status: {
        type: Sequelize.ENUM('available', 'wearing', 'washing', 'dry_cleaning', 'damaged', 'donated'),
        defaultValue: 'available',
        comment: '衣服状态',
      },
      wear_count: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        comment: '穿着次数',
      },
      last_worn_date: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: '最后穿着日期',
      },
      care_instructions: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '保养说明',
      },
      notes: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注',
      },
      is_favorite: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
        comment: '是否收藏',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      deleted_at: {
        type: Sequelize.DATE,
        allowNull: true,
      },
    });

    // 添加索引
    await queryInterface.addIndex('clothing', ['user_id']);
    await queryInterface.addIndex('clothing', ['category_id']);
    await queryInterface.addIndex('clothing', ['color']);
    await queryInterface.addIndex('clothing', ['season']);
    await queryInterface.addIndex('clothing', ['style']);
    await queryInterface.addIndex('clothing', ['status']);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('clothing');
  },
};
